<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/2/8
 * Time: 15:09
 */

namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Publics extends Controller{

    public function login(){
        $Request = Request::instance();
        if($Request->post()){
            $param = $Request->param();
            $this->checkLogin($param);
        }else{
            $rel = $Request->param();
            $message = isset($rel['message'])?$rel['message']:'';
            $this->assign('message',$message);
            return $this->fetch();
        }
    }
    public function checkLogin($param){
        $data = [
            'acc_name'=>$param['USER_NAME'],
            'acc_pwd'=>md5($param['USER_PAW']),
        ];
        $resouce = Db::table('user_acc')->where($data)->select();
        if(empty($resouce)){
            $this->redirect('Publics/login',['message' => '用户名或密码错误！']);
        }else{
            session('user',$resouce[0]);
            $this->setorgsession();
            $this->redirect('Index/index');
        }
    }

//    登录机构属性设置
    public function setorgsession(){
        $result  = Db::table('organization')
            ->where('org_code',session('user.org_code'))
            ->select();
//        var_dump($result);
        session('org',$result[0]);

    }

    public function register(){

    }

//    退出登录
    public function loginout(){
        session::clear();
        $this->redirect('Publics/login');
    }

    public function getsession(){
        var_dump(session('user'));
        var_dump(session('org'));
    }

}